package com.cnipr.patent;

import java.util.ArrayList;

import com.chinasofti.framework.base.AndroidUtils;
import com.chinasofti.framework.base.Utils;
import com.chinasofti.framework.dataaccess.Condition;
import com.chinasofti.framework.dataaccess.DataAccessUtils;
import com.chinasofti.framework.dataaccess.Filter;
import com.chinasofti.framework.dataaccess.Condition.Operation;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SearchActivity extends Activity implements Button.OnClickListener, OnFocusChangeListener{
    /** Called when the activity is first created. */
	
	private ArrayList<PatentSearchCondition> m_entities;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table_search_layout);
        
    	PatentSearchConditionDataAccess dataAccess = new PatentSearchConditionDataAccess();
        dataAccess.init(getApplicationContext());
        m_entities = new ArrayList<PatentSearchCondition>();
        Filter filter = new Filter();
        filter.addCondition(new Condition("visible", Operation.EQUAL, "true"));
        try {
        	dataAccess.load(filter, m_entities);
 		} 
        catch (Exception e) {
        	return;
        }
        
        fillConditionGroup(m_entities, 'A');
        fillConditionGroup(m_entities, 'B');
        fillConditionGroup(m_entities, 'C');
        fillConditionGroup(m_entities, 'D');
        fillConditionGroup(m_entities, 'E');
        fillConditionGroup(m_entities, 'F');
        fillConditionGroup(m_entities, 'G');
    
        Button searchButton = (Button)findViewById(R.id.btn_search);
        try {
        searchButton.setOnClickListener(this);
        }
        catch(Exception e) {
        }
        
    }
    

	private void fillConditionGroup(ArrayList<PatentSearchCondition> entities, char groupCode) {
		Filter filter = new Filter();
        filter.addCondition(new Condition("group", Operation.EQUAL, groupCode));
        ArrayList<PatentSearchCondition> group = new ArrayList<PatentSearchCondition>();
        DataAccessUtils.find(entities, filter, group);
        if(group.size() != 0) {
        	fillSearchTable(group);
        }
	}
    

	private void fillSearchTable(ArrayList<PatentSearchCondition> entities) {
		LinearLayout searchTable = createSearchTableLayout();
        for(int i = 0; i < entities.size(); i++) {
        	LinearLayout layout = createSearchConditionLayout();
        	searchTable.addView(layout);
        	TextView textView = createSearchTableTextView();
        	textView.setText(entities.get(i).getString("name") + ":"); 
        	layout.addView(textView);
        	EditText editText = createSearchTableEditText();
        	int id = entities.get(i).getInt(PatentSearchCondition.FIELD_ID);
        	editText.setId(id);
        	if(id == 3) {
        		editText.setText("CN85101067");
        	}
        	layout.addView(editText);
        	if(i != entities.size() -1) {
        		ImageView image = createSearchTableSpliteLine();
        		searchTable.addView(image);
        	}
        }
        try {
        	LinearLayout searchTableParent = (LinearLayout)findViewById(R.id.search_table_parent);
        	searchTableParent.addView(searchTable);
        }
        catch(Exception e) {
        }
    	
	}
    
    private LinearLayout createSearchTableLayout() {
    	LinearLayout layout = new LinearLayout(getApplicationContext());
    	layout.setOrientation(LinearLayout.VERTICAL);
    	LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
    			LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    	layoutParams.setMargins(18, 9, 18, 9);
    	layout.setLayoutParams(layoutParams);
    	Resources resources = getResources();
    	Drawable drawable = resources.getDrawable(R.drawable.input_group_bg);
    	layout.setBackgroundDrawable(drawable);
    	return layout;
    }
    
    private LinearLayout createSearchConditionLayout() {
    	LinearLayout layout = new LinearLayout(getApplicationContext());
    	layout.setOrientation(LinearLayout.HORIZONTAL);
    	LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    	layout.setLayoutParams(layoutParams);
    	layout.setPadding(6, 12, 6, 12);
    	return layout;
    }
    
    private ImageView createSearchTableSpliteLine() {
    	ImageView image = new ImageView(getApplicationContext());
    	Resources resources = getResources();
    	Drawable drawable = resources.getDrawable(R.drawable.line);
    	image.setImageDrawable(drawable);
    	LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 1);
    	image.setLayoutParams(layoutParams);
    	image.setClickable(false);
    	image.setScaleType(ScaleType.FIT_XY);
    	return image;
    }
    
    private TextView createSearchTableTextView() {
    	TextView textView = new TextView(getApplicationContext());
    	LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(120, LayoutParams.WRAP_CONTENT);
    	textView.setLayoutParams(layoutParams);
    	textView.setTextSize(16);
    	textView.setTextColor(Color.BLACK);
    	textView.setPadding(0, 0, 8, 0);
    	textView.setGravity(Gravity.RIGHT);
    	return textView;
    }
    
    private EditText createSearchTableEditText() {
    	EditText editText = new EditText(getApplicationContext());
    	LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
    			300, LayoutParams.WRAP_CONTENT);
    	editText.setLayoutParams(layoutParams);
    	editText.setPadding(15, 3, 15, 3);
    	editText.setSingleLine();
    	Resources resources = getResources();
    	Drawable drawable = resources.getDrawable(R.drawable.edit_text_bg);
    	editText.setBackgroundDrawable(drawable);
    	editText.setTextSize(16);
    	editText.setTextColor(Color.rgb(0, 100, 0));
    	editText.setOnFocusChangeListener(this);
    	return editText; 
    }

	@Override
	public void onFocusChange(View arg0, boolean arg1) {
		if(arg1) {
			arg0.setBackgroundDrawable(AndroidUtils.getResourceImage(this, R.drawable.edit_text_focus_bg));
		}
		else {
			arg0.setBackgroundDrawable(null);
		}
	}

	@Override
	public void onClick(View v) {
		if(v.equals(findViewById(R.id.btn_search))) {
			Intent intent = new Intent(this, SearchResultActivity.class);
			ArrayList<PatentSearchCondition> conditions = new ArrayList<PatentSearchCondition>();
			for(int i = 0; i < m_entities.size(); i++) {
				int id = m_entities.get(i).getInt(PatentSearchCondition.FIELD_ID);
				EditText edit = (EditText)findViewById(id);
				if(edit != null && edit.getText().length() != 0) {
					String text = edit.getText().toString();
					m_entities.get(i).set("value", text);
					conditions.add(m_entities.get(i));
				}
			}
			String xml = Utils.toXmlString(conditions, "condition");
			intent.putExtra("condition", xml);
			startActivity(intent);
		}
	}
    
}
